﻿using System;
using System.Web.Mvc;
using BLL.Security;
using BLL.Services.Messages;
using BLL.Services.Messages.Listing;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using Web.Infrastructure;
using Web.Infrastructure.Security;

namespace Web.Controllers
{
    [Authorization]
    public partial class MessagesController : FrontendController
    {
        private readonly IMessagesService _messagesService;

        private readonly IEnvironmentContext _environmentContext;

        public MessagesController(IMessagesService messagesService, IEnvironmentContext environmentContext)
        {
            _messagesService = messagesService;
            _environmentContext = environmentContext;
        }

        [ListingAction("Login")]
        public virtual ActionResult UserListing(int page, int size, GridSortOptions sort, ListingUserMessageFilter filter)
        {
            var user = _environmentContext.User;
            var model = _messagesService.GetMesagesByUser(page, user.UserId, size, sort.Column, sort.Direction != SortDirection.Descending ? 0 : 1, filter);
            return BackBoneJson(model);
        }

        public virtual ActionResult UserListingTemplate()
        {
            return PartialView(MVC.Messages.Views.UsersListingTemplate);
        }

        public virtual ActionResult MessageListing(int? senderId, int page = 1, int size = 10)
        {
            if (senderId != null)
            {
                var user = _environmentContext.User;
                _messagesService.UpdateNotReadMessage(senderId.Value,user.UserId, DateTime.Now);
                var model = _messagesService.ListingMessages(user.UserId, senderId.Value, page, size);
                model.UserId = user.UserId;
               
                return BackBoneJson(model);
            }
            return View(MVC.Shared.Views.Empty);
        }

        public virtual ActionResult MessageListingTemplate()
        {
            return PartialView(MVC.Messages.Views.MessageListingTemplate);
        }

        public virtual ActionResult MessageListingEmptyTemplate()
        {
            return PartialView(MVC.Messages.Views.MessageListingEmptyTemplate);
        }

        public virtual ActionResult UserListingEmptyTemplate()
        {
            return PartialView(MVC.Messages.Views.UserListingEmptyTemplate);
        }

        [HttpPost]
        public virtual ActionResult AddMessage(int recepientId, string message)
        {
            var model = new Messages
                {
                    DateOfSend = DateTime.Now,
                    Message = message,
                    RecipientId = recepientId,
                    SenderId = _environmentContext.User.UserId
                };

            _messagesService.Create(model);

            return Json(message);
        }

        public virtual ActionResult Save(int senderId, int recipientId, string message)
        {
            var model = new Messages
                {
                    DateOfSend = DateTime.Now,
                    Message = message,
                    RecipientId = recipientId,
                    SenderId = senderId
                };

            _messagesService.Create(model);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public virtual ActionResult Show()
        {

            return null;
        }

        public virtual ActionResult MessageTemplate()
        {
            return PartialView();
        }

        public virtual ActionResult SendMessageTemplate()
        {
            return PartialView(MVC.Messages.Views.SendMessageTemplate);
        }

    }
}
